﻿using System;
using System.Web;
using System.Data;
using SharedLibraries;
using SharedLibraries.Constants;
using EntityObject;
using DataAccessObject;
using System.Web.Security;
using ThangNM_LIB.FUNC;
using ThangNM_LIB.EO;

namespace ThangNM.PublicInterface
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["secret"] == null)
                    Response.Redirect("~/Account/Login.aspx?Return_Url=" + Server.UrlEncode(Request.AppRelativeCurrentExecutionFilePath + "?" + Request.QueryString));
                else
                {
                    String strCookie = Security.Login.Decrypt(Convert.ToString(Session["secret"]), Security.Base64Encode(Request.ServerVariables["remote_addr"]));
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(strCookie);
                    //AccountEO _AccountEO = new AccountEO();
                    //_AccountEO = (AccountEO)Commons.Json2Object(ticket.UserData, _AccountEO.GetType());
                    lblsUsername.Text = ticket.Name;
                    //if (_LoginEO.tCookiesExpires < DateTime.Now)
                    //{
                    //    Response.Redirect("~/Account/Login.aspx?Return_Url=" + Server.UrlEncode(Request.AppRelativeCurrentExecutionFilePath + "?" + Request.QueryString));
                    //}
                    //String IPAddress = _LoginEO.sIP;
                    //if (IPAddress.ToString().Trim() != Request.ServerVariables["remote_addr"].ToString().Trim())
                    //    Response.Redirect("~/Account/Login.aspx?Return_Url=" + Server.UrlEncode(Request.AppRelativeCurrentExecutionFilePath + "?" + Request.QueryString));
                }
                if (!IsPostBack)
                    BindData();
                else
                    hfNameTabActive.Value = Request.Form[hfNameTabActive.UniqueID];
            }
            catch
            {
                Session["secret"] = null;
                Response.Redirect("~/Account/Login.aspx?Return_Url=" + Server.UrlEncode(Request.AppRelativeCurrentExecutionFilePath + "?" + Request.QueryString));
            }
        }

        protected void tCheckNotification_Tick(object sender, EventArgs e)
        {
            BindData();
        }

        protected void BindData()
        {
            try
            {
                TempEO objTemp = (new TempDAO()).SelectItem(Temp_PK_iTempID_C.NOTIFICATION);
                lblContent.Text = objTemp.sContent;
                //DataTable dt = new DataTable();
                //dt = (DataTable)JsonEx.Json2Object(objTemp.sContent, dt.GetType());
                //objTemp = (new TempDAO()).SelectItem(Temp_PK_iTempID_C.tREQUEST);

                //dt.Columns.Add(new DataColumn("sContent", Type.GetType("System.String")));
                //foreach (DataRow dr in dt.Rows)
                //{
                //    switch (Convert.ToInt32(dr["PK_iTable"]))
                //    {
                //        case ConstEx.TableID_C.File:
                //            dr["sContent"] = "<a href=\"#\">" +
                //                                    "<div>" +
                //                                        "<i class=\"fa fa-download fa-fw\"></i> " + dr["iTotal"] + " New File" +
                //                                        "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                //                                    "</div>" +
                //                                "</a>";
                //            break;
                //        case ConstEx.TableID_C.Media:
                //            dr["sContent"] = "<a href=\"#\">" +
                //                                    "<div>" +
                //                                        "<i class=\"fa fa-smile-o fa-fw\"></i> " + dr["iTotal"] + " New Media" +
                //                                        "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                //                                    "</div>" +
                //                                "</a>";
                //            break;
                //        case ConstEx.TableID_C.QuestionAnswer:
                //            dr["sContent"] = "<a href=\"#\">" +
                //                                    "<div>" +
                //                                        "<i class=\"fa fa-question fa-fw\"></i> " + dr["iTotal"] + " New Question" +
                //                                        "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                //                                    "</div>" +
                //                                "</a>";
                //            break;
                //        case ConstEx.TableID_C.Answer:
                //            dr["sContent"] = "<a href=\"#\">" +
                //                                    "<div>" +
                //                                        "<i class=\"fa fa-comment fa-fw\"></i> " + dr["iTotal"] + " New Answer" +
                //                                        "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                //                                    "</div>" +
                //                                "</a>";
                //            break;
                //        case ConstEx.TableID_C.Quiz:
                //            dr["sContent"] = "<a href=\"#\">" +
                //                                    "<div>" +
                //                                        "<i class=\"fa fa-trophy fa-fw\"></i> " + dr["iTotal"] + " New Quiz" +
                //                                        "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                //                                    "</div>" +
                //                                "</a>";
                //            break;
                //        case ConstEx.TableID_C.Topic:
                //            dr["sContent"] = "<a href=\"#\">" +
                //                                    "<div>" +
                //                                        "<i class=\"fa fa-copy fa-fw\"></i> " + dr["iTotal"] + " New Topic" +
                //                                        "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                //                                    "</div>" +
                //                                "</a>";
                //            break;
                //        case ConstEx.TableID_C.Category:
                //            dr["sContent"] = "<a href=\"#\">" +
                //                                    "<div>" +
                //                                        "<i class=\"fa fa-copy fa-fw\"></i> " + dr["iTotal"] + " New Category" +
                //                                        "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                //                                    "</div>" +
                //                                "</a>";
                //            break;
                //    }
                //}
                //if (objTemp != null)
                //{
                //    DataRow drNew = dt.NewRow();
                //    drNew["sContent"] = "<a href=\"#\">" +
                //                        "<div>" +
                //                            "<i class=\"fa fa-refresh\"></i> Cập nhật vào lúc" +
                //                            "<span class=\"pull-right text-muted small\">" + Convert.ToDateTime(objTemp.sContent).ToString(FormatEx.Date_Time.Format_DateTime) + " </span>" +
                //                        "</div>" +
                //                    "</a>";
                //    dt.Rows.Add(drNew);
                //}
                //rptNotification.DataSource = dt;
                //rptNotification.DataBind();
            }
            catch (Exception ex)
            {
                (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false);
            }
        }
    }
}